<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends CommonController{

	public $address;

	public $orders;

	public $goods_cart;

	public $product;

	public $comments;

	public function _initialize(){

		$this->address = D('address');

		$this->orders = M('orders');

		$this->goods_cart = M('goods_cart as a');

		$this->product = M('product');

		$this->comments = D('comments');

	}



	public function order(){

		$field = 'order_id,cart_ids,status';
		if(!empty(session('user_id'))){
			$orders = $this->orders->field($field)->where(array('ueer_id'=>session('user_id')))->select();
			if($orders){
				foreach($orders as $key => $val){
					$orders[$key]['cart_id'] = explode(',',$val['cart_ids']);
					foreach($orders[$key]['cart_id'] as $ke => $va){
						$orders[$key]['goods_info'][$ke] = $this->goods_cart->field('a.pro_id,a.color_id,a.size_id,a.number,b.pro_name,c.price')->join('rl_product as b on a.pro_id = b.pro_id')->join('rl_pro_inventory as c on a.pro_id = c.pro_id and a.color_id = c.color_id and a.size_id = c.size_id')->where(array('cart_id'=>$va))->find();
					}
				}
			}
			//待付款
			$wait_pay = $this->orders->field('count(*)wait_pay')->where(array('user_id'=>session('user_id'),'status'=>0))->select();

			//待发货
			$wait_start = $this->orders->field('count(*)wait_start')->where(array('user_id'=>session('user_id'),'status'=>1))->select();

			//待评价
			$wait_judge = $this->orders->field('count(*)wait_judge')->where(array('user_id'=>session('user_id'),'status'=>3))->select();

			$status = array_merge($wait_pay[0],$wait_start[0],$wait_judge[0]);
			// dump($status);die;
		}else{
			$this->redirect('Login/login');
		}
		
		// dump(session('user_id'));die;
		// dump($orders);die;
		$this -> assign(array(
			'orders'		=>	$orders,
			'status'		=>	$status,
		));
		
		$this -> display();
		
	}

	public function address(){

		if(session('user_id')==''){
			$this->redirect('Login/login');
		}
		if(IS_POST){
			$info = ['code' => 0 , 'status' => false , 'info' => ''];
			if(I('post.status')==1){

				if(!$this->address->create()){
					$info['info'] = $this->address->getError();
					$this->ajaxReturn($info);
				}else{
					$result = $this->address->add();
					if(!$result){
						$info['info'] = '保存失败';
						$this->ajaxReturn($info);
					}else{
						$info['code'] = 1;
						$info['status'] = true;
						$info['info'] = '保存成功';
						if(I('post.defau')==1){
							$defau = M('default');
							$defau->where(array('table'=>'address'))->save(array('default_id'=>$result));
						}
						$this->ajaxReturn($info);
					}
					$this->ajaxReturn($info);
				}

			}elseif(I('post.status')==2){
				$result = $this->address->where(array('address_id'=>I('post.add_id')))->delete();
				if(!$result){
					$info['info'] = '删除失败';
					$this->ajaxReturn($info);
				}else{
					$info['code'] = 1;
					$info['status'] = true;
					$info['info'] = '删除成功';
					$this->ajaxReturn($info);	
				}
			}elseif(I('post.status')==3){
				$defau = M('default');
				$result = $defau->where(array('table'=>'address'))->save(array('default_id'=>I('post.address_id')));
				if(!$result){
					$info['info'] = '设置失败';
					$this->ajaxReturn($info);
				}else{
					$info['code'] = 1;
					$info['status'] = true;
					$info['info'] = '设置成功';
					$this->ajaxReturn($info);
				}
			}
			
		}else{
			$addre = $this->address->where(array('user_id'=>session('user_id')))->select();
			// var_dump($addre);die;
			$this->assign(array(
				'addre'		=>	$addre,
			));
			$this->display();
		}
		
	}

	public function address_edit(){
		if(IS_POST){
			$info = ['code' => 0 , 'status' => false , 'info' => ''];

			if(!$this->address->create()){
				$info['info'] = $this->address->getError();
				$this->ajaxReturn($info);
			}else{
				$result = $this->address->save();
				if(!$result){
					$info['info'] = '修改失败';
					$this->ajaxReturn($info);
				}else{
					$info['code'] = 1;
					$info['status'] = true;
					$info['info'] = '保存成功';
					if(I('post.defau')==1){
						$defau = M('default');
						$defau->where(array('table'=>'address'))->save(array('default_id'=>$result));
					}
					$this->ajaxReturn($info);
				}
				$this->ajaxReturn($info);
			}
			

		}else{
			$address_id = I('get.address_id');
			$result = $this->address->where(array('address_id'=>$address_id))->find();
			$default = M('default');
			$result['defau'] = $default->where(array('table'=>'address'))->find();


			$this->assign(array(
				'result'		=>	$result,
			));
			$this->display();
		}
	}

	public function comments(){
		if(!empty(session('user_id'))){

			if(IS_POST){
				$info = ['code' => 0 ,'status' => false , 'info' => ''];
				$data = I('post.');
				// var_dump($data);
				
				// var_dump($data);

				if($data['mode']==1){
					//评价
					if($data['describe'] == 0 || $data['transport'] == 0 || $data['attitude'] == 0){
						$info['info'] = '商品评价最低一星';
						$this->ajaxReturn($info);
					}else{
						if(!$this->comments->create()){
							$info['info'] = $this->comments->getError();
							$this->ajaxReturn($info);
						}else{
							$where['order_id'] = $data['order_id'];
							$where['pro_id'] = $data['pro_id'];
							$se = $this->comments->where($where)->find();
							if(!$se){
								$result = $this->comments->add();
								if(!$result){
									$info['info'] = '评价失败';
									$this->ajaxReturn($info);
								}else{
									$info['code'] = 1;
									$info['status'] = true;
									$info['info'] = '评价成功';
									$this->ajaxReturn($info);
								}
							}else{
								$info['info'] = '你已评价过该商品,可以进行追评';
								$this->ajaxReturn($info);
							}
							
						}
					}
				}elseif($data['mode']==2){
					//追评
					$comments = M('comments');
					$where['order_id'] = $data['order_id'];
					$where['pro_id'] = $data['pro_id'];
					$add_com = $comments->where($where)->save(array('add_comment'=>$data['comments']));
					if(!$add_com){
						$info['info'] = '追评失败';
						$this->ajaxReturn($info);
					}else{
						$info['code'] = 1;
						$info['status'] = true;
						$info['info'] = '追评成功';
						$this->ajaxReturn($info);
					}
				}

				
				
			}else{
				$data = I('get.');

				$order = $this->orders->where(array('order_id'=>$data['order_id']))->find();

				$order['cart_id'] = explode(',',$order['cart_ids']);
				
				

				$goods = $this->product->where(array('pro_id'=>$data['pro_id']))->find();

				$this->assign(array(
					'goods'		=>	$goods,
					'order' 	=>	$order,
				));
				$this->display();
			}
		}else{
			$this->redirect('Login/login');
		}
	}

	public function get_upload()
	{	
		//检查文件夹
		$path = './Public/Admin/upload/img/';
		//文件夹不存在则创建文件夹
		if(!file_exists($path)){
			mkdir($path,0777,true);
			$path = rtrim($path,'/').'/';
		}

		$config = array(

			//限制文件大小
			'maxSize'	=>	3145728,
			//设置保存目录
			'rootPath'	=>	$path,
			//上传文件的保存名称
			'saveName'	=>	array('uniqid',''),
			//文件上传的格式类型
			'exts'		=>	array('jpg','gif','png','jpeg'),
			'autoSub'	=>	ture,
			'subName'	=>	array('date','Ymd'),
		);
		$upload = new \Think\Upload($config);//实例化上传类
		//文件上传,支持多文件上传
		
		$info[0] = $upload->upload(array('main_img'=>$_FILES['main_img']));
		$info[1] = $upload->upload(array('detail_img'=>$_FILES['detail_img']));
		$info[2] = $upload->upload(array('detail_info_img'=>$_FILES['detail_info_img']));
		if(!empty($_FILES['water'])){
			$info[3] = $upload->upload(array('water'=>$_FILES['water']));
		}
		$result = array();
		if(!$info){
			return $result;
		}else{
			foreach($info as $key => $val){
				foreach($val as $va){
					$result[$key][] = $path.$va['savepath'].$va['savename'];
				}
			}
			return $result;
		}
	}

}






?>